//https://leetcode.cn/problems/ba-zi-fu-chuan-zhuan-huan-cheng-zheng-shu-lcof/submissions/548840890/

class Solution {
public:
    int myAtoi(string str) {
        int i = 0;
        //获取空格
        while (str[i] == ' ')
        {
            i++;
        }

        //获取正负号
        int flag = 1;
        if (str[i] == '-')
        {
            flag = -1;
            i++;
        }
        else if (str[i] == '+')
            i++;

        //无效字符
        if ('0' > str[i] || str[i] > '9' || str[i] == '\0')
            return 0;


        long long num = 0;
        while (str[i] >= '0' && str[i] <= '9')
        {
            num *= 10;
            num += str[i++] - '0';

            if (num > INT_MAX)
                return flag > 0 ? INT_MAX : INT_MIN;
        }

        return num * flag;

    }
};
